package com.eastfair.auth.config;

import com.eastfair.boot.core.handler.AbstractGlobalExceptionHandler;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.core.exception.code.ExceptionCode;
import com.eastfair.core.utils.StrPool;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理
 *
 * @author ligang
 * @date 2020年01月02日17:19:27
 */
@Configuration
@ConditionalOnClass({Servlet.class, DispatcherServlet.class})
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@RestControllerAdvice(annotations = {RestController.class, Controller.class})
@Slf4j
public class ExceptionConfiguration extends AbstractGlobalExceptionHandler {
    /**
     * 其他异常
     *feign.FeignException$BadRequest: [400 ] during [POST] to [http://project-core/dataIsolation/queryList] [ProjectCoreServiceFeign#queryDataIsolaction(DataIsolationDTO)]: [{"code":40018,"data":null,"msg":"登录场景为空","path":"/dataIsolation/queryList","extra":null,"timestamp":1658825222133,"errorMsg":"登录场景为空","isSuccess":false}]
     *
     *
     * feign.FeignException$InternalServerError: [500 ] during [POST] to [http://project-core/dataIsolation/queryList] [ProjectCoreServiceFeign#queryDataIsolaction(DataIsolationDTO)]: [{"timestamp":"2022-07-26 20:21:27","status":500,"error":"Internal Server Error","message":"","path":"/dataIsolation/queryList"}]
     *
     *
     * @param ex 异常
     */
    @ExceptionHandler(FeignException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public R<?> feignExceptionHandler(Exception ex) {
//        System.out.println(getExceptionAllinformation(ex));
//        log.info("sssssssssssssssis "+getExceptionAllinformation(ex));
        log.warn("feignException:", ex);
//        log.info(getExceptionAllinformation(ex));
        if (ex.getCause() instanceof BizException) {
            return this.bizException((BizException) ex.getCause());
        }
        return R.result(ExceptionCode.SYSTEM_BUSY.getCode(), null, ExceptionCode.SYSTEM_BUSY.getMsg(), ex.getMessage()).setPath(getPath());
    }
    private String getPath() {
        String path = StrPool.EMPTY;
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            path = request.getRequestURI();
        }
        return path;
    }
}
